/* eslint-disable unicorn/prevent-abbreviations */
import express, { Request, Response } from 'express'
import bodyParser from 'body-parser'
import { AvatarService } from './services/avatar'
import { client } from './db/redis'

const app = express()
const PORT = 3000
const avatarService = new AvatarService(client)

app.use(bodyParser.json())

app.get('/avatar/:email', async (request: Request, res: Response) => {
  try {
    const { email } = request.params

    if (!email) {
      return res.status(400).json({ error: 'Missing "email" parameter.' })
    }

    const avatarResult = await avatarService.create(email)
    res.json(avatarResult)
  } catch (error) {
    console.error('Error processing request:', error)
    res.status(500).json({ error: 'Internal Server Error' })
  }
})

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`)
})
